package br.elsa.survey.services.listeners;

import javax.enterprise.event.Observes;
import javax.inject.Inject;

import br.elsa.survey.domain.questions.Question;
import br.elsa.survey.services.events.QuestionCreatedEvent;
import br.elsa.survey.services.events.QuestionRemovedEvent;
import br.elsa.survey.statistics.Statistics;

/**
 * Component that listen to business events over {@link Question} entities
 * 
 * @author diego
 * @since 2010-10-22
 */
public class QuestionEventListener {

	@Inject
	private Statistics statisticsService;

	public QuestionEventListener() {

	}

	/**
	 * Used by test classes to inject Mock dependencies
	 * 
	 * @param statistics
	 */
	public QuestionEventListener(Statistics statistics) {
		this.statisticsService = statistics;
	}

	/**
	 * Refresh statistics data when a new {@link Question} is created in the
	 * system
	 * 
	 * @param event
	 *            event holding the created question
	 */
	public void onQuestionCreated(@Observes QuestionCreatedEvent event) {
		statisticsService.loadStatistics();
	}

	/**
	 * Refresh statistics data when a {@link Question} is removed from the
	 * system
	 * 
	 * @param event
	 *            event holding the removed question
	 */
	public void onQuestionRemoved(@Observes QuestionRemovedEvent event) {
		statisticsService.loadStatistics();
	}

}
